package com.lottery.service.pc28.lobbyService.impl;

import com.lottery.dao.pc28.accoutDao.UserDao;
import com.lottery.dao.pc28.bettingDao.BettingLimitationDao;
import com.lottery.dao.pc28.lobbyDao.BettinginfoDao;
import com.lottery.dao.pc28.lobbyDao.PlaywayinfoDao;
import com.lottery.dao.pc28.statisticsDao.PlaywayMoneyDao;
import com.lottery.dao.pc28.statisticsDao.TotalMoneyDao;
import com.lottery.dao.pc28.statisticsDao.UserTop10Dao;
import com.lottery.dao.pc28.wealthDao.AccountDao;
import com.lottery.dao.pc28.wealthDao.AccountTransactionDao;
import com.lottery.dto.ResultDto;
import com.lottery.entity.BettingLimitation;
import com.lottery.service.pc28.lobbyService.BettingLimitationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class BettingLimitationServiceImpl implements BettingLimitationService {
	@Autowired
	private BettingLimitationDao bettingLimitation;
	@Autowired
	private AccountDao accountdao;
	@Autowired
	private BettinginfoDao bettingInfoDao;
	@Autowired
	private AccountTransactionDao accountTransactionDao;
	@Autowired
	private PlaywayinfoDao playwayInfoDao;
	@Autowired
	private PlaywayMoneyDao playwayMoneyDao;
	@Autowired
	private TotalMoneyDao totalMoneyDao;
	@Autowired
	private UserTop10Dao userTop10Dao;
	@Autowired
	private UserDao userDao;
	
	private final Logger logger=LoggerFactory.getLogger(this.getClass());
	
	@Override
	public ResultDto<BettingLimitation> selectMaxMinBet() {
		// TODO Auto-generated method stub
		ResultDto<BettingLimitation> resultDto = new ResultDto<BettingLimitation>();
		try {
			BettingLimitation selectMaxMinBet = bettingLimitation.selectMaxMinBet();
			
			logger.info("selectMaxMinBet信息={}",selectMaxMinBet);
			resultDto.setData(selectMaxMinBet);
			resultDto.setSuccess(true);
			resultDto.setMessage("查询成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resultDto.setSuccess(false);
			resultDto.setMessage("查询失败" + e.toString());
		}
		return resultDto;

	}

	@Override
	public ResultDto<BettingLimitation> updateMaxMinBet(BettingLimitation bettingController) {
		// TODO Auto-generated method stub
		ResultDto<BettingLimitation> resultDto = new ResultDto<BettingLimitation>();
		try {

			if ((bettingController.getBetMin()) < (bettingController.getBetMax())) {
				bettingLimitation.updateMaxMinBet(bettingController);
				resultDto.setSuccess(true);
				resultDto.setMessage("修改成功");
			} else {
				resultDto.setMessage("投注下限不能高于投注上限");
				resultDto.setSuccess(false);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resultDto.setSuccess(false);
			resultDto.setMessage("修改操作异常停止了");
		}
		return resultDto;
	}

/*	@Override
	public ResultDto<Bettinginfo> MaxMinBetAffirm(Bettinginfo bettinginfo) {
		// TODO Auto-generated method stub
		ResultDto<Bettinginfo> resultDto = new ResultDto<Bettinginfo>();
		BettingLimitation selectMaxMinBet = bettingLimitation.selectMaxMinBet();
		if (selectMaxMinBet != null) {
			AccountTransaction accountTransaction = new AccountTransaction();
			accountTransaction.setExpect(bettinginfo.getExpect());
			accountTransaction.setTransactionType(3);
			AccountTransaction totalAccountTransaction = bettingLimitation
					.getTotalAccountTransaction(accountTransaction);
			
			if (selectMaxMinBet.getBetMin() <= bettinginfo.getBettingmoney()
					&& totalAccountTransaction.getTransactionMoney()+bettinginfo.getBettingmoney() <= selectMaxMinBet.getBetMax()) {
				Account selectAccountByUserId = accountdao.selectAccountByUserId(bettinginfo.getUserId());
				if(selectAccountByUserId.getAccountFunds()>=bettinginfo.getBettingmoney()){
					List<Bettinginfo> list=new ArrayList<Bettinginfo>();
					list.add(bettinginfo);
					bettingInfoDao.insertBettinginfoList(list);
					selectAccountByUserId.setAccountFunds(selectAccountByUserId.getAccountFunds()-bettinginfo.getBettingmoney());
					accountdao.updateAccount(selectAccountByUserId);
					accountTransaction.setAccountId(selectAccountByUserId.getId());
					accountTransaction.setExpect(bettinginfo.getExpect());
					accountTransaction.setTransactionDate(new Date());
					accountTransaction.setTransactionMoney(bettinginfo.getBettingmoney());
					accountTransaction.setTransactionType(3);
					accountTransaction.setMoneyGo("购买彩票");
					accountTransaction.setMoneySources("用户帐户");
					accountTransaction.setFee(0);
					accountTransactionDao.insertAccountTransaction(accountTransaction);
					PlaywayMoney playwayMoney = new PlaywayMoney();
					playwayMoney.setBettingmoney(bettinginfo.getBettingmoney());
					playwayMoney.setExpect(bettinginfo.getExpect());
					playwayMoney.setIncome(0);
					playwayMoney.setPlaywayinfoId(bettinginfo.getPlaywayinfoId());
					playwayMoney.setWinTotalmoney(0);
					playwayMoneyDao.insertPlaywayMoney(playwayMoney);
					Playwayinfo selectPlaywayinfoByName = playwayInfoDao.selectPlaywayinfoByName(bettinginfo.getPlaywayName());
					TotalMoney totalMoney = new TotalMoney();
					totalMoney.setBettingmoney(bettinginfo.getBettingmoney());
					totalMoney.setExpect(bettinginfo.getExpect());
					totalMoney.setLotteryTypeId(selectPlaywayinfoByName.getId());
					totalMoney.setWinTotalmoney(0);
					totalMoney.setIncome(0);
					totalMoney.setTotalDate(new Date());
					totalMoneyDao.insertTotalMoney(totalMoney);
					UserTop10 userTop10 = new UserTop10();
					userTop10.setExpect(bettinginfo.getExpect());
					userTop10.setUserId(bettinginfo.getUserId());
					List<UserTop10> selectUserTop10ListByExpect = userTop10Dao.selectUserTop10ListByExpect(userTop10);
					
					if(selectUserTop10ListByExpect!=null&&selectUserTop10ListByExpect.size()>0){
					UserTop10 userTop=selectUserTop10ListByExpect.get(0);
					
					userTop.setUserBetTotalMoney(userTop.getUserBetTotalMoney()+bettinginfo.getBettingmoney());
					userTop10Dao.UpdateUserTop10(userTop);
					}else{
						UserTop10 userT = new UserTop10();
						userT.setExpect(bettinginfo.getExpect());
						System.out.println(bettinginfo.getBettingmoney());
						userT.setUserBetTotalMoney(bettinginfo.getBettingmoney());
						userT.setUserId(bettinginfo.getUserId());
						User selectId = userDao.selectId(bettinginfo.getUserId());
						userT.setUsername(selectId.getUserName());
						userT.setUserWinTotalMoney(0);
						userT.setIncome(0);
						System.out.println(userT);
						userTop10Dao.insertUserTop10(userT);
						}
					resultDto.setMessage("投注成功");
					resultDto.setSuccess(true);
					
				}else{
					resultDto.setMessage("帐户余额不足");
					resultDto.setSuccess(false);
				}

			} else {
				resultDto.setMessage("投注金额已超上下限");
				resultDto.setSuccess(false);
			}
		} else {
			resultDto.setMessage("商家未设置上下限,不能投注");
			resultDto.setSuccess(false);
		}

		return resultDto;

	}*/

	

	/*
	 * @Override public ResultDto<BettingLimitation>
	 * insertMaxMinBet(BettingLimitation bettingController) { // TODO
	 * Auto-generated method stub ResultDto<BettingLimitation> resultDto = new
	 * ResultDto<BettingLimitation>(); try {
	 * bettingLimitation.insertMaxMinBet(bettingController);
	 * resultDto.setSuccess(true); resultDto.setMessage("插入成功"); } catch
	 * (Exception e) { // TODO Auto-generated catch block e.printStackTrace();
	 * resultDto.setSuccess(false); resultDto.setMessage("插入失败"+e.toString()); }
	 * return resultDto; }
	 */

	/*
	 * public ResultDto<BettingController> selectMaxMinBetByUserId() { // TODO
	 * Auto-generated method stub ResultDto<BettingController> resultDto = new
	 * ResultDto<BettingController>(); try { List<BettingController>
	 * selectMaxMinBet = bettingKongzhiDao.selectMaxMinBet(); BettingController
	 * selectMaxMin=selectMaxMinBet.get(selectMaxMinBet.size()-1);
	 * resultDto.setData(selectMaxMin); resultDto.setMessage("200");
	 * resultDto.setSuccess(true); } catch (Exception e) { // TODO
	 * Auto-generated catch block e.printStackTrace();
	 * resultDto.setMessage("400"+e.toString()); resultDto.setSuccess(false); }
	 * return resultDto;
	 * 
	 * 
	 * }
	 */

	/*
	 * @Override public ResultDto<BettingController>
	 * updateMaxMinBet(BettingController bettingController) { // TODO
	 * Auto-generated method stub ResultDto<BettingController> resultDto = new
	 * ResultDto<BettingController>(); try {
	 * bettingKongzhiDao.updateMaxMinBet(bettingController);
	 * resultDto.setMessage("200"); resultDto.setSuccess(true); } catch
	 * (Exception e) { // TODO Auto-generated catch block e.printStackTrace();
	 * resultDto.setMessage("400"+e.toString()); resultDto.setSuccess(false); }
	 * return resultDto;
	 * 
	 * }
	 */

	/*
	 * @Override public ResultDto<BettingController>
	 * insertMaxMinBet(BettingController bettingController) { // TODO
	 * Auto-generated method stub ResultDto<BettingController> resultDto = new
	 * ResultDto<BettingController>(); try {
	 * bettingKongzhiDao.insertMaxMinBet(bettingController);
	 * resultDto.setMessage("200"); resultDto.setSuccess(true); } catch
	 * (Exception e) { // TODO Auto-generated catch block e.printStackTrace();
	 * resultDto.setMessage("400"+e.toString()); resultDto.setSuccess(false); }
	 * return resultDto;
	 * 
	 * }
	 */

	/**
	 * 投注金额上下限的验证
	 */
	/*
	 * public ResultDto<BettingController> checkBettingMoney(Bettinginfo
	 * bettinginfo) { ResultDto<BettingController> resultDto = new
	 * ResultDto<BettingController>(); BettingController selectMaxMinBet =
	 * bettingKongzhiDao.selectMaxMinBet(bettinginfo.getExpect()); final int
	 * BETMIN=selectMaxMinBet.getBetMin(); final int
	 * BETMAX=selectMaxMinBet.getBetMax(); AccountTransaction accountTransaction
	 * = new AccountTransaction();
	 * accountTransaction.setExpect(bettinginfo.getExpect());
	 * 
	 * Account selectAccountByUserId =
	 * accountDao.selectAccountByUserId(bettinginfo.getUserId());
	 * accountTransaction.setAccountId(selectAccountByUserId.getId());
	 * AccountTransaction selectSumAccountTransaction =
	 * bettingKongzhiDao.selectSumAccountTransaction(accountTransaction);
	 * if(bettinginfo.getBettingmoney()>=BETMIN&&selectSumAccountTransaction.
	 * getTransactionMoney()<=BETMAX){
	 * 
	 * if(selectAccountByUserId.getAccountFunds()>=bettinginfo.getBettingmoney()
	 * ){ resultDto.setSuccess(true); resultDto.setMessage("投注金额在限定范围内");
	 * 
	 * }else{ resultDto.setSuccess(false); resultDto.setMessage("帐户余额不足"); }
	 * 
	 * }else{ resultDto.setSuccess(false); resultDto.setMessage("投注金额超过上下限"); }
	 * return resultDto;
	 * 
	 * 
	 */
	/*
	 * ResultDto<Bettinginfo> resultDto = new ResultDto<Bettinginfo>(); try {
	 * BettingController selectMaxMinBet =
	 * bettingKongzhiDao.selectMaxMinBet(bettinginfo.getExpect()); Account
	 * selectAccountByUserId =
	 * accountDao.selectAccountByUserId(bettinginfo.getUserId());
	 * if(selectAccountByUserId.getAccountFunds()-bettinginfo.getBettingmoney()>
	 * =0){ final int BETMIN=selectMaxMinBet.getBetMin(); final int
	 * BETMAX=selectMaxMinBet.getBetMax(); AccountTransaction accountTransaction
	 * = new AccountTransaction();
	 * accountTransaction.setAccountId(selectAccountByUserId.getId());
	 * accountTransaction.setExpect(bettinginfo.getExpect()); AccountTransaction
	 * sumAccountTransaction =
	 * accountTransactionDao.getSumAccountTransaction(accountTransaction);
	 * 
	 * if(bettinginfo.getBettingmoney()>=BETMIN&&sumAccountTransaction.
	 * getTransactionMoney()<=BETMAX){
	 * selectAccountByUserId.setAccountFunds(selectAccountByUserId.
	 * getAccountFunds()-bettinginfo.getBettingmoney());
	 * selectAccountByUserId.setUserId(bettinginfo.getUserId());
	 * accountDao.updateAccount(selectAccountByUserId);
	 * accountTransaction.setTransactionDate(new Date());
	 * accountTransaction.setTransactionMoney(bettinginfo.getBettingmoney());
	 * accountTransactionDao.insertAccountTransaction(accountTransaction);
	 * List<Bettinginfo> bettinginfoList=new ArrayList<Bettinginfo>();
	 * bettinginfoList.add(bettinginfo);
	 * bettinginfoDao.insertBettinginfoList(bettinginfoList);
	 * resultDto.setMessage("投注成功"); resultDto.setSuccess(true);
	 * 
	 * 
	 * } }else{ resultDto.setMessage("帐户余额不足"); resultDto.setSuccess(false);
	 * 
	 * } } catch (Exception e) { // TODO Auto-generated catch block
	 * e.printStackTrace(); resultDto.setSuccess(false);
	 * resultDto.setMessage("投注失败了"); } return resultDto;
	 */

	/*
	 * Bettinginfo selectSumBettingInfo =
	 * bettingKongzhiDao.selectSumBettingInfo();
	 * 
	 * final int BETMIN=selectMaxMinBetByUserId.getBetMin(); final int
	 * BETMAX=selectMaxMinBetByUserId.getBetMax(); try {
	 * if(bettinginfo.getBettingmoney()>=BETMIN&&bettinginfo.getBettingmoney()+
	 * selectSumBettingInfo.getBettingmoney()<=BETMAX){ int userId =
	 * bettinginfo.getUserId(); Account selectAccountByUserId =
	 * accountDao.selectAccountByUserId(userId);
	 * if(selectAccountByUserId.getAccountFunds()-bettinginfo.getBettingmoney()>
	 * =0){ selectAccountByUserId.setAccountFunds(selectAccountByUserId.
	 * getAccountFunds()-bettinginfo.getBettingmoney());
	 * selectAccountByUserId.setUserId(userId);
	 * accountDao.updateAccount(selectAccountByUserId); AccountTransaction
	 * accountTransaction = new AccountTransaction();
	 * accountTransaction.setAccountId(selectAccountByUserId.getId());
	 * accountTransaction.setTransactionDate(new Date());
	 * accountTransaction.setTransactionMoney(bettinginfo.getBettingmoney());
	 * accountTransactionDao.insertAccountTransaction(accountTransaction);
	 * List<Bettinginfo> bettinginfoList=new ArrayList<Bettinginfo>();
	 * bettinginfoList.add(bettinginfo);
	 * bettinginfoDao.insertBettinginfoList(bettinginfoList);
	 * resultDto.setMessage("投注成功"); resultDto.setSuccess(true);
	 * 
	 * }else{ resultDto.setMessage("帐户余额不足"); resultDto.setSuccess(false);
	 * 
	 * } }else{ resultDto.setMessage("投注金额超出范围"); resultDto.setSuccess(false);
	 * 
	 * } } catch (Exception e) { // TODO Auto-generated catch block
	 * e.printStackTrace(); resultDto.setSuccess(false);
	 * resultDto.setMessage("投注失败了"+e); } return resultDto;
	 * 
	 * 
	 * }
	 */

}
